﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ASMS.Excel2DbTool.Utils
{
    public class WaiyunAirline
    {
        class Node
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int R { get; set; }
            public int G { get; set; }
            public int B { get; set; }
            /// <summary>
            /// 0:area,1:company,2:customer
            /// </summary>
            public int Category { get; set; }
        }

        class Edge
        {
            public int Id { get; set; }
            public int SourceId { get; set; }
            public int TargetId { get; set; }
            public float Weight { get; set; }
        }
        public static void Run(string filePath)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            List<string> comList = new List<string>();
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string company = Convert.ToString(dataTable.Rows[index][1]).Trim().Replace("&", "&amp;");

                if (comList.Contains(company))
                {
                    continue;
                }
                comList.Add(company);
            }

            List<Node> nodes = new List<Node>();
            List<Edge> edges = new List<Edge>();
            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string area = Convert.ToString(dataTable.Rows[index][0]).Trim().Replace("&", "&amp;");
                string company = Convert.ToString(dataTable.Rows[index][1]).Trim().Replace("&", "&amp;");
                string customer = Convert.ToString(dataTable.Rows[index][3]).Trim().Replace("&", "&amp;");
                int peroid = DateTime.FromOADate(Convert.ToDouble(Convert.ToString(dataTable.Rows[index][2]))).Month;
                int day = DateTime.FromOADate(Convert.ToDouble(Convert.ToString(dataTable.Rows[index][2]))).Day;
                int ticketCount = Convert.ToInt32(dataTable.Rows[index][4]);
                if (peroid != 11 || ticketCount < 10)
                    continue;

                Node areaNode = null;
                Node companyNode = null;
                Node customerNode = null;
                areaNode = nodes.SingleOrDefault(p => p.Name == area);
                if (areaNode == null)
                {
                    areaNode = new Node();
                    areaNode.Id = nodes.Count;
                    areaNode.Name = area;
                    areaNode.R = 255;
                    areaNode.G = 22;
                    areaNode.B = 22;
                    areaNode.Category = 0;
                    nodes.Add(areaNode);
                    Console.WriteLine(string.Format("新区域:{0}", area));
                }

                companyNode = nodes.SingleOrDefault(p => p.Name == company);
                if (companyNode == null)
                {
                    companyNode = new Node();
                    companyNode.Id = nodes.Count;
                    companyNode.Name = company;
                    companyNode.R = 22;
                    companyNode.G = 22;
                    companyNode.B = 255;
                    companyNode.Category = 1;
                    nodes.Add(companyNode);
                    Console.WriteLine(string.Format("新公司:{0}", company));
                }

                customerNode = nodes.SingleOrDefault(p => p.Name == customer);
                if (customerNode == null)
                {
                    customerNode = new Node();
                    customerNode.Id = nodes.Count;
                    customerNode.Name = customer;
                    customerNode.R = 255;
                    customerNode.G = 255;
                    customerNode.B = 22;
                    if (comList.Contains(customer))
                    {
                        customerNode.Category = 1;
                    }
                    else
                    {
                        customerNode.Category = 2;
                    }
                    nodes.Add(customerNode);
                    Console.WriteLine(string.Format("新客户:{0}", customer));
                }

                Edge area2companyEdge = edges.SingleOrDefault(p => p.SourceId == areaNode.Id && p.TargetId == companyNode.Id);
                if (area2companyEdge == null)
                {
                    area2companyEdge = new Edge();
                    area2companyEdge.Id = edges.Count;
                    area2companyEdge.SourceId = areaNode.Id;
                    area2companyEdge.TargetId = companyNode.Id;
                    area2companyEdge.Weight = 1.0f;
                    edges.Add(area2companyEdge);
                }
                Edge company2customerEdge = edges.SingleOrDefault(p => p.SourceId == companyNode.Id && p.TargetId == customerNode.Id);
                if (company2customerEdge == null)
                {
                    company2customerEdge = new Edge();
                    company2customerEdge.Id = edges.Count;
                    company2customerEdge.SourceId = companyNode.Id;
                    company2customerEdge.TargetId = customerNode.Id;
                    company2customerEdge.Weight = 1.0f;
                    edges.Add(company2customerEdge);
                }
            }


            StringBuilder builder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gexf xmlns=\"http://www.gexf.net/1.2draft\" version=\"1.2\" xmlns:viz=\"http://www.gexf.net/1.2draft/viz\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd\"><meta lastmodifieddate=\"2014-01-30\"><creator>Gephi 0.8.1</creator><description></description></meta><graph defaultedgetype=\"undirected\" mode=\"static\"><attributes class=\"node\" mode=\"static\"><attribute id=\"modularity_class\" title=\"Modularity Class\" type=\"integer\"></attribute></attributes><nodes>");
            foreach (var node in nodes)
            {
                builder.AppendFormat("<node id=\"{0}\" label=\"{1}\"><attvalues><attvalue for=\"modularity_class\" value=\"{2}\"></attvalue></attvalues><viz:size value=\"28.685715\"></viz:size><viz:position x=\"-266.82776\" y=\"299.6904\" z=\"0.0\"></viz:position><viz:color r=\"{3}\" g=\"{4}\" b=\"{5}\"></viz:color></node>", node.Id,
                    node.Name,
                    node.Category,
                    node.R,
                    node.G,
                    node.B);
            }
            builder.Append("</nodes><edges>");
            foreach (var edge in edges)
            {
                builder.AppendFormat("<edge id=\"{0}\" source=\"{1}\" target=\"{2}\" weight=\"{3}\"><attvalues></attvalues></edge>",
                    edge.Id,
                    edge.SourceId,
                    edge.TargetId,
                    edge.Weight);
            }
            builder.Append("</edges></graph></gexf>");

            System.IO.File.WriteAllText(string.Format("{0}\\data-force-airline.gexf.xml", AppDomain.CurrentDomain.BaseDirectory),
                builder.ToString());
            Console.WriteLine(string.Format("转换完毕,edges:{0}", edges.Count));
        }
    }
}
